package com.chenxingnet.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.chenxingnet.dao.AfterMapper;
import com.chenxingnet.dto.After;
import com.chenxingnet.service.AfterService;
import com.chenxingnet.tools.MobilePage;
import com.chenxingnet.tools.Page;
@Service("afterService")
public class AfterServiceImpl implements AfterService{

	@Resource
	private AfterMapper afterMapper;
	
	public int addAfter(After after) {
		
		return afterMapper.insertSelective(after);
	}

	public void updateAfter(After after) {
		afterMapper.updateByPrimaryKeySelective(after);
		
	}

	public void deleteAfter(Integer aid) {
		afterMapper.updateAfter(aid);
		
	}

	public After showAfterById(Integer aid) {
		
		return afterMapper.selectByPrimaryKey(aid);
	}

	public void selectAllByCount(HttpServletRequest request, Model model) {
		
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<After> afterList = new ArrayList<After>();
		//按条件搜索
		String soua = request.getParameter("souafter");
		if(soua != null){
			List<After> list = afterMapper.souAfter(soua);
			model.addAttribute("afterList", list);
			request.setAttribute("afterList", list);
		}else{
			//分页查询
			int totalCount = (int)afterMapper.getCount();
			if(pageNow != null){
				page = new Page(totalCount, Integer.valueOf(pageNow));
				afterList = this.afterMapper.selectAllByCount(page.getStartPos(), page.getPageSize());
			}else{
				page = new Page(totalCount, 1);
				afterList = this.afterMapper.selectAllByCount(page.getStartPos(), page.getPageSize());
			
			}
			model.addAttribute("afterList", afterList);
			model.addAttribute("page", page);
			
			request.setAttribute("afterList", afterList);
			request.setAttribute("page", page);
			
		}
		
	}
	
	/**
	 * 移动端分页查询
	 */
	public void mobSelectAllByCount(HttpServletRequest request) {
		
		String pageNow = request.getParameter("pageNow");
		MobilePage mobilePage = null;
		List<After> afterList = new ArrayList<After>();
		//按条件搜索
		String soua = request.getParameter("souafter");
		if(soua != null){
			List<After> list = afterMapper.souAfter(soua);
			request.setAttribute("afterList", list);
		}else{
			//分页查询
			int totalCount = (int)afterMapper.getCount();
			if(pageNow != null){
				mobilePage = new MobilePage(totalCount, Integer.valueOf(pageNow));
				afterList = this.afterMapper.selectAllByCount(mobilePage.getStartPos(), mobilePage.getPageSize());
			}else{
				mobilePage = new MobilePage(totalCount, 1);
				afterList = this.afterMapper.selectAllByCount(mobilePage.getStartPos(), mobilePage.getPageSize());
			
			}
			
			request.setAttribute("afterList", afterList);
			request.setAttribute("mobilePage", mobilePage);
			
		}
		
	}

}
